package com.mcptt.db.loader.address;

import java.util.List;
import java.util.Vector;

import android.annotation.TargetApi;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DbConst;
import com.mcptt.db.model.UserInfo;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AddressListLoader implements LoaderManager.LoaderCallbacks<Cursor> 
{
    private final static String THIS_FILE = "AddressListLoader";
    private Context ctx = null;
    private AddressListLoaderCb callback = null;
    private int addrType = UserInfo.UserType_User;
    public AddressListLoader(Context ctx,int addrType,AddressListLoaderCb callback)
    {
	this.ctx = ctx;
	this.addrType = addrType;
	this.callback = callback;
    }
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) 
    {
//	String selection = DbConst.COL_UserType + "=?";
//	return new CursorLoader(this.ctx, DbConst.ADDRESS_URI, DBProvider.ADDRESS_FULL_PROJECTION,selection, new String[] { "" + addrType },null);
		return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) 
    {
	Log.debug(this.getClass(),"onLoadFinished ");
	if (data == null
		|| data.getCount() <= 0)
	{
	    return;
	}
	Log.debug(this.getClass(),"onLoadFinished Cursor.getCount() = " + data.getCount());
	List<UserInfo> list = new Vector<UserInfo>();
	Cursor cursor = data;
	while (cursor.moveToNext()) 
	{  
	    UserInfo info = new UserInfo(cursor);  
	    list.add(info);
	} 
	this.callback.onLoadFinished(list);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) 
    {
	this.callback.onLoaderReset();
    }
}
